package com.honey.compilation.sql.ddl;

import com.honey.compilation.sql.SqlKeyWord;
import com.honey.compilation.sql.SqlScript;
import com.honey.core.builder.CharacterBuilder;
import com.honey.core.builder.CharacterBuilderFactory;
import com.honey.core.compiler.CompilationException;

/**
 * Set 事务级访问模型,SQL 92中定义如下: <br />
 * &lt;set transaction statement&gt;    ::= SET TRANSACTION &lt;transaction mode&gt; <br />
 * &lt;transaction mode&gt;    ::=&lt;transaction access mode&gt; <br />
 * &lt;transaction access mode&gt;    ::=   READ ONLY | READ WRITE <br />
 * 
 * 设定只读事务.( 很多数据库都不支持这个特性 )
 * @author Administrator
 * 
 */
public final class SetTransactionAccessMode extends SetTransaction implements SqlScript {
	
	/** 事务的读取模型 */
	private AccessMode accessMode ;

	/**
	 * 设定数据的读取模型
	 * @param accessMode
	 */
	public void setAccessMode(AccessMode accessMode) {
		this.accessMode = accessMode;
	}
	
	/**
	 * 设定事务只读事务
	 */
	public void setReadonly(){
		this.accessMode = accessMode.READ_ONLY;
	} 
	
	/**
	 * 设定事务为读写事务
	 */
	public void setReadwrite (){
		this.accessMode = accessMode.READ_WRITE;
	} 
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.core.dom.sql.ddl.SetTransaction#compiledContent(int)
	 */
	@Override
	public CharacterBuilder compiledContent(int indentLevel ) throws CompilationException {
		CharacterBuilder answer = CharacterBuilderFactory.createC16StringBuilder() ;
		if(accessMode != null){
			answer.append( super.compiledContent(indentLevel) )
			.append(accessMode)
			.append(SqlKeyWord.OPERATION_SEMICOLON)
			;
		}
		return answer;
	}
	
	/**
	 * 事务的读写模型
	 * @author Administrator
	 *
	 */
	public enum AccessMode{
		
		/** 只读事务 */
		READ_ONLY("READ ONLY "),
		
		/** 可读可写事务 */
		READ_WRITE("READ WRITE "),
		;
		
		private final String access;
		
		/**
		 * 构造函数
		 * @param access 
		 */
		private AccessMode (String access){
			this.access =  access;
		}
		
		@Override
		public String toString() {
			
			return access;
		}
	}
}
